{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Fetching API Key from Environment Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# Fetch the OpenAI API key from environment variables\n",
    "api_key = os.environ.get('OPENAI_API_KEY')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check if api_key has loaded\n",
    "# print(api_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pipeline for Converting Raw Unstructured Data into a QA Chain\n",
    "\n",
    "1. **Loading**: Initially, the data needs to be loaded. Unstructured data can be sourced from various platforms. Utilize the LangChain Integration Hub to explore the complete range of loaders. Each loader outputs the data as a LangChain Document.\n",
    "\n",
    "2. **Splitting**: Text splitters segment the Documents into specified sizes.\n",
    "\n",
    "3. **Storage**: A storage solution, often a vector store, is used to house and sometimes embed the splits.\n",
    "\n",
    "4. **Retrieval**: The application fetches the splits from the storage, usually based on embeddings similar to the input question.\n",
    "\n",
    "5. **Generation**: A Language Model (LLM) generates an answer using a prompt that incorporates both the question and the retrieved data.\n",
    "\n",
    "6. **Conversation (Extension)**: To facilitate multi-turn conversations, Memory can be added to the QA chain.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Q/A pipeline RAG](https://python.langchain.com/assets/images/qa_flow-9fbd91de9282eb806bda1c6db501ecec.jpeg)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1: Loading the Document\n",
    "\n",
    "In this initial step, we focus on loading the document into our system. This is a crucial phase as the quality of data loaded will significantly impact the subsequent stages of the pipeline. Here, you can use various loaders available in the LangChain Integration Hub to import your unstructured data as a LangChain Document."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import WebBaseLoader\n",
    "\n",
    "# Initialize the WebBaseLoader with the URL of the document to be loaded\n",
    "loader = WebBaseLoader(\"https://my.clevelandclinic.org/health/diseases/10946-cavities\")\n",
    "\n",
    "# Load the document and store it in the 'data' variable\n",
    "data = loader.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(page_content='Cavities (Tooth Decay): Symptoms, Causes & Treatment800.223.2273100 Years of Cleveland ClinicMyChartNeed Help?GivingCareersSearchClevelandClinic.orgFind A DoctorLocations & DirectionsPatients & VisitorsHealth LibraryInstitutes & DepartmentsAppointmentsHome/Health Library/Diseases & Conditions/CavitiesAdvertisementAdvertisementCavitiesCavities are holes, or areas of tooth decay, that form in your teeth surfaces. Causes include plaque buildup, eating lots of sugary snacks and poor oral hygiene. Treatments include dental fillings, root canal therapy and tooth extraction. The sooner you treat a cavity, the better your chance for a predictable outcome and optimal oral health.ContentsArrow DownOverviewSymptoms and CausesDiagnosis and TestsManagement and TreatmentPreventionOutlook / PrognosisLiving WithAdditional Common QuestionsOverviewCavities can form on the crown or root of your tooth. You might not feel a cavity until it reaches the dentin or pulp.What is a cavity?A cavity is a hole in a tooth that develops from tooth decay. Cavities form when acids in your mouth wear down (erode) your tooth’s hard outer layer (enamel). Anyone can get a cavity. Proper oral hygiene and regular dental cleanings can prevent cavities.Another name for tooth cavities is dental caries.Types of cavitiesCavities can start on any tooth surface. Here are common types of cavities and where they occur:Smooth surface: This slow-growing cavity dissolves tooth enamel. You can prevent it — and sometimes reverse it — with proper oral hygiene. People in their 20s often develop this form of tooth decay between their teeth.Pit and fissure decay: Cavities form on the top part of your tooth’s chewing surface. Decay also can affect the front side of your back teeth. Pit and fissure decay tends to start during the teenage years and progresses quickly.Root decay: Adults who have receding gums are more prone to root decay. Gum recession exposes your teeth roots to dental plaque and acid. Root decay is difficult to prevent and treat. (If you’re prone to gum recession, ask your dentist if you should schedule an appointment with a periodontist.)How common are cavities?More than 80% of Americans have at least one cavity by the time they enter their mid-30s. Cavities are one of the most common chronic diseases affecting people of all ages.Who might get a cavity?Tooth decay can happen at any age, although cavities are more common in children. This is because many children don’t brush properly or regularly enough and they tend to consume more sugary foods and drinks.Many adults also get cavities. Sometimes, new decay develops around the edges of cavities treated in childhood. Adults are also more likely to have receding gums. This condition exposes your teeth roots to plaque, which can cause cavities.AdvertisementCleveland Clinic is a non-profit academic medical center. Advertising on our site helps support our mission. We do not endorse non-Cleveland Clinic products or services. PolicySymptoms and CausesWhat are the signs of cavities?Tooth decay on the outer enamel surface doesn’t usually cause pain or symptoms. You’re more likely to experience symptoms as decay reaches beyond the enamel into the dentin and pulp.Cavity symptoms include:Bad breath or a bad taste in your mouth.Bleeding gums or other signs of gum disease.Facial swelling.Toothache or mouth pain.Tooth sensitivity to hot or cold foods or drinks.Tooth decay stagesCavities can affect all layers of your tooth.There are five main tooth decay stages:Demineralization: During this first stage, you may notice small, white, chalky spots on your tooth. This is due to the breakdown of minerals in your tooth enamel.Enamel decay: Left untreated, tooth decay progresses and continues to break down your enamel. At this point, cavities (holes) may become noticeable. White spots may turn to a light brownish color.Dentin decay: Dentin is the layer just beneath your tooth enamel. It’s much softer than your enamel. So, once plaque and bacteria reach this layer, cavities form faster. At this stage, you may notice teeth sensitivity. The spots on your teeth may also turn to a darker brown.Pulp damage: Your tooth pulp is the innermost layer of your tooth. It contains nerves and blood vessels that transport nutrients and keep your tooth alive. When cavities reach your pulp, you may feel pain. You may also start to notice redness and swelling in the gums around your tooth. The spots on your tooth may turn darker brown or black.Abscessed tooth: Left untreated, a deep cavity can cause infection. This results in a pocket of pus that forms at the tip of your tooth root (periapical abscess). Symptoms may include pain that radiates into your jaw or face. You may also develop facial swelling and swollen lymph nodes in your neck. At this point, a tooth abscess can spread to surrounding tissues and other areas of your body. In rare cases, infection can even spread to your brain or to your bloodstream (sepsis).What causes cavities?Many factors play a role in the development of cavities.Here’s how it works:Bacteria in your mouth feed on sugary, starchy foods and drinks (fruit, candy, bread, cereal, sodas, juice and milk). The bacteria convert these carbohydrates into acids.Bacteria, acid, food and saliva mix to form dental plaque. This sticky substance coats your teeth.Without proper brushing and flossing, acids in plaque dissolve tooth enamel, creating cavities, or holes, in the enamel surface.What are the risk factors for cavities?Certain factors increase your risk of cavities, including:Dry mouth (xerostomia). Certain conditions (like Sjögren’s syndrome), or medications (like antidepressants) can make you more likely to develop dry mouth.Consuming sugary, starchy foods or drinks and snacking between meals.Family history of tooth decay.Gum recession.Previous radiation therapy to treat head and neck cancer.Are cavities contagious?While you can’t “catch” a cavity, the bacteria that cause cavities can pass from one person to another. In turn, any bacteria you pick up from another person (from kissing, for instance) can lead to tooth decay and other oral health issues over time.AdvertisementDiagnosis and TestsHow are cavities diagnosed?Twice-a-year dental checkups are the best way to catch cavities early, before they worsen or grow larger. A dentist will use a number of instruments to examine your teeth. A tooth with a cavity will feel softer when your dentist probes it.Your dentist may also take dental X-rays. These images show cavities before the decay is visible.Management and TreatmentHow do you get rid of cavities?Tooth decay treatment depends on the severity of your condition.Cavity treatments include:Fluoride.Fillings.Root canal therapy.Tooth extraction.FluorideIn the very early stages of tooth decay, fluoride treatments can repair damaged enamel — a process called remineralization. This can reverse the early signs of cavities. You may need prescription toothpaste and mouthwash, as well as fluoride treatments at the dental office.Dental fillingsOnce a hole forms in your tooth, a dentist drills out the decayed tissue and fills the hole. Dental fillings consist of composite resin (a tooth-colored material), silver amalgam or gold.Root canal therapyRoot canal therapy relieves pain from advanced tooth decay. Endodontists (specialists who treat issues that affect a tooth’s root) usually perform the procedure. During root canal treatment, an endodontist removes the tooth pulp, then fills the canals and pulp chamber with gutta-percha (a special filling material). In some cases, you might also need a dental crown to strengthen the affected tooth.Tooth extractionIf root canal therapy isn’t possible, your healthcare provider may recommend tooth extraction (pulling the tooth). You may need a dental bridge or dental implant to replace a pulled permanent tooth. Your dentist can tell you about your specific options.AdvertisementPreventionHow can I prevent cavities?Proper oral hygiene, including regular brushing and flossing, can get rid of plaque, acids and cavity-causing bacteria. Good teeth and gum care includes:Brushing your teeth with a soft-bristled brush and fluoride toothpaste at least twice a day, and preferably after every meal.Cutting back on sugary, starchy foods and drinks.Daily flossing to get rid of food and plaque stuck between your teeth.Dental checkups at least twice a year. (You might need more frequent visits if you’re prone to tooth decay, gum disease or other oral health issues.)Dental sealants to protect the top chewing surfaces of your teeth.Outlook / PrognosisWhat can I expect if I have cavities?When tooth decay goes untreated for too long, you can lose a large portion of your tooth and need an extraction. Advanced tooth decay can lead to a severe infection inside your tooth and under your gums (tooth abscess). This infection can spread throughout your body. Rarely, infection from a tooth abscess can be fatal.What’s the outlook for people with cavities?Most people with cavities don’t experience any long-term problems. Because cavities develop slowly, it’s important to get regular dental checkups. Fluoride treatments can stop tooth decay in its early stages. Once tooth decay advances to the root, you risk losing the tooth or developing a painful abscess (infection).Living WithWhen should I call a dentist?You should schedule an appointment with a dentist if you experience:Bleeding gums.Difficulty chewing.Signs of infection.Swollen face.Toothache or mouth pain.What questions should I ask my dentist?Here are some questions you might want to ask your dentist:Why did I get a cavity?What is the best treatment for this cavity?What steps can I take to lower the risk of getting more cavities?Should I look out for signs of complications?What oral health products do you recommend?Additional Common QuestionsWhat does a cavity look like?Though cavities are hard to see in the early stages, you might notice a small, white, chalky area on your tooth enamel. As the cavity worsens, you may see brown or black spots on your tooth.Cavities vs. stainsCavities and stains both cause tooth discoloration, so it can be easy to confuse the two. Generally, cavities are darker in color compared to stains. Additionally, stains tend to affect several teeth. But if you see one tiny spot on one tooth, chances are it’s a cavity.If you think you have a cavity, call a dentist right away. Prompt, timely treatment is key.What does a cavity feel like?It depends on the severity of tooth decay. For instance, if you have a very small cavity that only affects your enamel, you won’t feel it. But when decay reaches the inner layers of your tooth, you may experience pain or sensitivity to heat, cold or sweets. (Teeth sensitivity doesn’t always mean you have a cavity. But it’s a good idea to get it checked out, just in case.)What happens if you have a cavity for a long time?Small tooth cavities turn into large cavities over time. As decay worsens, it affects the deeper layers of your tooth. This can lead to pain, infection and ultimately, tooth loss. Early treatment is essential for long-term oral health.Can a cavity heal on its own?Yes, in the very early stages, you may be able to reverse cavities. Tooth enamel can repair itself. If a cavity is very small, and it hasn’t spread beyond your enamel yet, then you may be able to remineralize your tooth with fluoride treatments and improved oral hygiene.Keep in mind, though — if the cavity spreads to your dentin, you’ll need a filling, crown or other dental restoration to fix it.If a cavity hurts, is it too late?If a cavity is painful, it means the decay has spread to the deeper layers of your tooth. At this stage, you’ll need a dentist to repair the cavity. When treated quickly, a dentist may be able to save your tooth.Most dentists prefer to save your natural teeth. If a cavity results in an abscessed tooth, or the damage is too significant, you may need a tooth extraction. A dentist can discuss your treatment options in detail.A note from Cleveland ClinicIf you have signs of a cavity, don’t hesitate to see a dentist for care. They’ll check your tooth and recommend the best course of action. If you catch tooth decay in its earliest stage, you might not even need a filling. That’s why it’s so important to seek care right away.Dental visits can be stressful, especially when you don’t know what’s going to happen. But establishing care with a provider you trust can help take the fear out of sitting in the dental chair. Your dentist will teach you tips for brushing and flossing that can help you avoid new cavities and keep you smiling for years to come.Medically ReviewedLast reviewed by a Cleveland Clinic medical professional on 03/27/2023.Learn more about our editorial process.ReferencesAdvertisementCleveland Clinic is a non-profit academic medical center. Advertising on our site helps support our mission. We do not endorse non-Cleveland Clinic products or services. PolicyAdAppointments 216.444.8500Appointments & LocationsRequest an AppointmentFacebookTwitterYouTubeInstagramLinkedInPinterestSnapchatActionsAppointments & AccessAccepted InsuranceEvents CalendarFinancial AssistanceGive to Cleveland ClinicPay Your Bill OnlineRefer a PatientPhone DirectoryVirtual Second OpinionsVirtual VisitsBlog, News & AppsConsult QDHealth EssentialsNewsroomMyClevelandClinicMyChartAbout Cleveland Clinic100 Years of Cleveland ClinicAbout UsLocationsQuality & Patient SafetyOffice of Diversity & InclusionPatient ExperienceResearch & InnovationsCommunity CommitmentCareersFor EmployeesResources for Medical ProfessionalsSite Information & PoliciesSend Us FeedbackSite MapAbout this WebsiteCopyright, Reprint & LicensingWebsite Terms of UsePrivacy PolicyNotice of Privacy PracticesNon-Discrimination Notice9500 Euclid Avenue, Cleveland, Ohio 44195 | 800.223.2273 | © 2023 Cleveland Clinic. All Rights Reserved.\\n', metadata={'source': 'https://my.clevelandclinic.org/health/diseases/10946-cavities', 'title': 'Cavities (Tooth Decay): Symptoms, Causes & Treatment', 'description': 'A cavity is a hole, or area of decay, in your tooth. Cavities form when acids in your mouth erode (wear down) your tooth enamel — your tooth’s hard, outer layer.', 'language': 'en'})]\n"
     ]
    }
   ],
   "source": [
    "# Display the content of the loaded document\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Splitting the Document into Chunks\n",
    "\n",
    "In this step, we aim to divide the loaded document into smaller, manageable chunks, also known as splits. This is essential for easier processing and retrieval in the subsequent stages of the pipeline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Encoding 'cl100k_base'>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tiktoken\n",
    "\n",
    "# Set up token encoding for the GPT-3.5 Turbo model\n",
    "tiktoken.encoding_for_model('gpt-3.5-turbo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer = tiktoken.get_encoding('cl100k_base')\n",
    "\n",
    "# Define a function to calculate the token length of a given text\n",
    "def tiktoken_len(text):\n",
    "    tokens = tokenizer.encode(\n",
    "        text,\n",
    "        disallowed_special=()\n",
    "    )\n",
    "    return len(tokens)\n",
    "\n",
    "tiktoken_len(\"Dentin decay: Dentin is the layer just beneath your tooth enamel.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "\n",
    "# Initialize the text splitter with specified parameters\n",
    "text_splitter = RecursiveCharacterTextSplitter(\n",
    "    chunk_size = 100,\n",
    "    chunk_overlap = 20,\n",
    "    length_function = tiktoken_len\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Split the loaded document into smaller chunks\n",
    "chunks = text_splitter.split_documents(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "37"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check the total number of chunks generated\n",
    "len(chunks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Storing the Vector Embeddings in Vector Database\n",
    "\n",
    "In this step, we will store the vector embeddings of the generated chunks into a vector database. This is crucial for efficient retrieval and further processing of the data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. **Database Storage**: To facilitate future retrieval of our document splits, it's essential to store them in a database.\n",
    "\n",
    "2. **Embedding Model**: To convert our document splits into vector embeddings, we require an embedding model.\n",
    "\n",
    "3. **Vector Store**: Finally, the vector embeddings and documents will be stored in a vector store. For this purpose, we will be using ChromaDB.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![img](https://python.langchain.com/assets/images/qa_data_load-70fac3ea6593b986613784dc056df21a.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings import HuggingFaceEmbeddings\n",
    "\n",
    "# Specify the model name and additional arguments\n",
    "model_name = \"sentence-transformers/all-MiniLM-L6-v2\"\n",
    "model_kwargs = {'device' : 'cpu'}\n",
    "encode_kwargs = {'normalize_embeddings': False}\n",
    "\n",
    "# Initialize HuggingFace Embeddings\n",
    "hf = HuggingFaceEmbeddings(\n",
    "    model_name = model_name,\n",
    "    model_kwargs = model_kwargs,\n",
    "    encode_kwargs = encode_kwargs\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "384\n"
     ]
    }
   ],
   "source": [
    "embed = hf.embed_documents(texts=['h','e'])\n",
    "\n",
    "# Print the length of one of the embeddings to check its dimensions\n",
    "print(len(embed[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install ChromaDB package using pip\n",
    "# !pip install chromadb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.vectorstores import Chroma\n",
    "\n",
    "# Initialize Chroma vector database with chunks and HuggingFace embeddings\n",
    "vectordb = Chroma.from_documents(chunks, hf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(page_content='your bloodstream (sepsis).What causes cavities?Many factors play a role in the development of cavities.Here’s how it works:Bacteria in your mouth feed on sugary, starchy foods and drinks (fruit, candy, bread, cereal, sodas, juice and milk). The bacteria convert these carbohydrates into acids.Bacteria, acid, food and saliva mix to form dental plaque. This sticky substance coats your teeth.Without proper brushing and flossing, acids in plaque dissolve tooth', metadata={'source': 'https://my.clevelandclinic.org/health/diseases/10946-cavities', 'title': 'Cavities (Tooth Decay): Symptoms, Causes & Treatment', 'description': 'A cavity is a hole, or area of decay, in your tooth. Cavities form when acids in your mouth erode (wear down) your tooth enamel — your tooth’s hard, outer layer.', 'language': 'en'}),\n",
       " Document(page_content='treated in childhood. Adults are also more likely to have receding gums. This condition exposes your teeth roots to plaque, which can cause cavities.AdvertisementCleveland Clinic is a non-profit academic medical center. Advertising on our site helps support our mission. We do not endorse non-Cleveland Clinic products or services. PolicySymptoms and CausesWhat are the signs of cavities?Tooth decay on the outer enamel surface doesn’t usually cause pain or symptoms. You’re more likely to experience symptoms as', metadata={'source': 'https://my.clevelandclinic.org/health/diseases/10946-cavities', 'title': 'Cavities (Tooth Decay): Symptoms, Causes & Treatment', 'description': 'A cavity is a hole, or area of decay, in your tooth. Cavities form when acids in your mouth erode (wear down) your tooth enamel — your tooth’s hard, outer layer.', 'language': 'en'}),\n",
       " Document(page_content='innermost layer of your tooth. It contains nerves and blood vessels that transport nutrients and keep your tooth alive. When cavities reach your pulp, you may feel pain. You may also start to notice redness and swelling in the gums around your tooth. The spots on your tooth may turn darker brown or black.Abscessed tooth: Left untreated, a deep cavity can cause infection. This results in a pocket of pus that forms at the tip of your tooth root (periapical abscess).', metadata={'source': 'https://my.clevelandclinic.org/health/diseases/10946-cavities', 'title': 'Cavities (Tooth Decay): Symptoms, Causes & Treatment', 'description': 'A cavity is a hole, or area of decay, in your tooth. Cavities form when acids in your mouth erode (wear down) your tooth enamel — your tooth’s hard, outer layer.', 'language': 'en'})]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Perform a similarity search on the vector database\n",
    "vectordb.similarity_search('bleeding gums', k=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Retrieve and Generate\n",
    "\n",
    "In this step, we will retrieve the relevant chunks from the vector database and generate answers using a language model. This is the final step in the pipeline, bringing all the previous steps together to produce a coherent QA chain."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'query': 'How can I prevent cavity in my tooth?',\n",
       " 'result': \"To prevent cavities in your teeth, here are some steps you can take:\\n\\n1. Brush your teeth twice a day with fluoride toothpaste. Make sure to brush all surfaces of your teeth and along the gumline.\\n\\n2. Floss daily to remove plaque and food particles from between your teeth where a toothbrush can't reach.\\n\\n3. Limit your consumption of sugary and acidic foods and drinks, as they can contribute to tooth decay. If you do have them, try to rinse your mouth with water afterwards.\\n\\n4. Visit your dentist regularly for check-ups and professional cleanings. They can identify early signs of cavities and provide preventive treatments like fluoride applications.\\n\\n5. Consider using a mouthwash that contains fluoride to help strengthen your tooth enamel.\\n\\n6. Drink fluoridated water or use fluoride supplements if it is not available in your local water supply.\\n\\n7. Practice good oral hygiene habits and teach them to your children from an early age.\\n\\nRemember, prevention is key when it comes to cavities. By maintaining good oral hygiene and seeking regular dental care, you can greatly reduce your risk of developing cavities.\"}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import RetrievalQA\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "# Initialize a language model with ChatOpenAI\n",
    "llm = ChatOpenAI(model_name= 'gpt-3.5-turbo', temperature=0.6)\n",
    "\n",
    "# Initialize a RetrievalQA chain with the language model and vector database retriever\n",
    "qa_chain = RetrievalQA.from_chain_type(llm, retriever= vectordb.as_retriever())\n",
    "\n",
    "# Pass a query to the QA chain to generate an answer\n",
    "qa_chain({'query' : 'How can I prevent cavity in my tooth?'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Change the query to what you want to ask the LLM\n",
    "query = 'What is enamel decay'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'query': 'What is enamel decay',\n",
       " 'result': 'Enamel decay refers to the breakdown or deterioration of the outermost layer of your tooth called enamel. This occurs due to the acids produced by bacteria in your mouth, which can be caused by poor oral hygiene, a diet high in sugary or acidic foods, or certain medical conditions. Enamel decay is the second stage of tooth decay and can lead to the formation of cavities or holes in the tooth. If left untreated, the decay can progress to affect the deeper layers of the tooth, leading to more severe dental problems.'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa_chain({'query' : query})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Next Steps:\n",
    "\n",
    "- Integrate prompt engineering \n",
    "\n",
    "- Use Prompt Template from langchain"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
